%Jim data analysis m-file

clear all;
clear global data;
close all;
clc;

subj = load('data_jim.mat');
x = linspace(0,100,2000);
trials = [-20 -10 0 40 70 105 151 185 220 261 320 373 400 440 481 500 550 560 580 610 650 700];


torque = subj.data.torque.average(2:end-1,:);
[I,J] = find(torque>2);


%% EMG
fs = 1000; %Hz
MH = subj.data.EMG.MH.typical(2:end-1,:);
MH = lowpass(MH,fs,50);
RF = subj.data.EMG.RF.typical(2:end-1,:);
RF = lowpass(RF,fs,50);
VL = subj.data.EMG.VL.typical(2:end-1,:);
VL = lowpass(VL,fs,50);
AL = subj.data.EMG.AL.typical(2:end-1,:);
AL = lowpass(AL,fs,50);

%normalization EMG
max_MH = max(MH(:,1));
max_RF = max(RF(:,1));
max_AL = max(AL(:,1));
max_VL = max(VL(:,1));
MH = MH/max_MH;
RF = RF/max_RF;
AL = AL/max_AL;
VL = VL/max_VL;

% figure(2);
% plot(MH(:,1),'b')
% hold on;
% plot(MH(:,2),'c')
% plot(MH(:,3),'y')
% plot(MH(:,17),'r')
% plot(MH(:,18),'m')
% plot(MH(:,22),'g')
% title('MH')
% 
% figure(3);
% plot(VL(:,1),'b')
% hold on;
% plot(VL(:,2),'c')
% plot(VL(:,3),'y')
% plot(VL(:,17),'r')
% plot(VL(:,18),'m')
% plot(VL(:,22),'g')
% title('VL')
% 
% figure(10);
% plot(AL(:,1),'b')
% hold on;
% plot(AL(:,2),'c')
% plot(AL(:,3),'y')
% plot(AL(:,17),'r')
% plot(AL(:,18),'m')
% plot(AL(:,22),'g')
% title('AL')
% 
% figure(11);
% plot(RF(:,1),'b')
% hold on;
% plot(RF(:,2),'c')
% plot(RF(:,3),'y')
% plot(RF(:,17),'r')
% plot(RF(:,18),'m')
% plot(RF(:,22),'g')
% title('RF')

ind = find(J==2); 
ind = 1100:1600;
MH_out = MH(I(ind(1)):I(ind(end)),:);
sum_MH = sum(MH_out,1);
RF_out = RF(I(ind(1)):I(ind(end)),:);
sum_RF = sum(RF_out,1);
VL_out = VL(I(ind(1)):I(ind(end)),:);
sum_VL = sum(VL_out,1);
AL_out = AL(I(ind(1)):I(ind(end)),:);
sum_AL = sum(AL_out,1);

figure(20); hold on;
plot(trials(3:17),sum_RF(3:17)/sum_RF(2)*100,'ok','MarkerSize',8);
plot(trials([1:2,18:22]),sum_RF([1:2,18:22])/sum_RF(2)*100,'ok','MarkerSize',8)
title('RF')

figure(21); hold on;
plot(trials(3:17),sum_AL(3:17)/sum_AL(2)*100,'ok','MarkerSize',8);
plot(trials([1:2,18:22]),sum_AL([1:2,18:22])/sum_AL(2)*100,'ok','MarkerSize',8)
title('AL')

figure(22); hold on;
plot(trials(3:17),sum_MH(3:17)/sum_MH(2)*100,'ok','MarkerSize',8);
plot(trials([1:2,18:22]),sum_MH([1:2,18:22])/sum_MH(2)*100,'ok','MarkerSize',8)
title('MH')

figure(23); hold on;
plot(trials(3:17),sum_VL(3:17)/sum_VL(2)*100,'ok','MarkerSize',8);
plot(trials([1:2,18:22]),sum_VL([1:2,18:22])/sum_VL(2)*100,'ok','MarkerSize',8)
title('VL')

%% Kinematics

hip_offset = [.7 .7 .9 .7 .9];%zeros(1,5); %thigh_sag - pel_sag;
knee_offset = zeros(1,5); %thigh_sag - shank_sag;
ankle_offset = [1 1 .8 1 1]; %zeros(1,5); %foot_sag - shank_sag;

hip = subj.data.hip_angle.left.average + hip_offset(1);
knee = subj.data.knee_angle.left.average + knee_offset(1);
ankle = subj.data.ankle_angle.left.average + ankle_offset(1);
hip_circ = subj.data.hip_circ.left.average;
pelv_circ = subj.data.pelvic_circ.left.average;

knee_vel_TO = subj.data.knee_vel_TO.left.normal;
circumduction = subj.data.hip_circ.left.ROM + subj.data.pelvic_circ.left.ROM;
steplength_l = subj.data.steplength.left.normal;
steplength_r = subj.data.steplength.right.normal;
toeclear = subj.data.toeclear.left.normal;
knee_vel = subj.data.knee_vel.left.max;

rhip = subj.data.hip_angle.right.average;
rknee = subj.data.knee_angle.right.average;
rankle = subj.data.ankle_angle.right.average;


hip = hip*180/pi; knee = knee*180/pi; ankle = ankle*180/pi; 
rhip = rhip*180/pi+22; rknee= rknee*180/pi -25; rankle= rankle*180/pi +58;

figure(1);
subplot(4,1,1); hold on;
plot(x,hip(:,3),'g');
plot(x,hip(:,17),'c');
plot(x,hip(:,18),'m');
plot(x,hip(:,2),'LineStyle',':');
ylabel('Hip Angle (deg)');

subplot(4,1,2);hold on;
plot(x,knee(:,3),'g');
plot(x,knee(:,17),'c');
plot(x,knee(:,18),'m');
plot(x,knee(:,2),'LineStyle',':');
ylabel('Knee Angle (deg)');
axis([0 100 0 50]);

subplot(4,1,3);hold on;
plot(x,ankle(:,2),'LineStyle',':');
plot(x,ankle(:,3),'g');
plot(x,ankle(:,17),'c');
plot(x,ankle(:,18),'m');
ylabel('Ankle Angle (deg)');
axis([0 100 -20 40]);
lh = legend('Baseline','Init. Exp.','Late Exp.','Washout');
set(lh,'FontSize',8)

subplot(4,1,4); hold on;
plot(x(2:end-1),torque(:,3),'g');
ylabel('Torque (Nm)');
xlabel('Gait Cycle (%)')
set(gcf,'Position',[568 -167 544 769]);

figure(2);
subplot(3,1,1); hold on;
plot(x,pelv_circ(:,2),'LineStyle',':');
plot(x,pelv_circ(:,3),'g');
plot(x,pelv_circ(:,17),'c');
ylabel('Pelvic Obliquity (deg)');


subplot(3,1,2); hold on;
plot(x,hip_circ(:,2),'LineStyle',':');
plot(x,hip_circ(:,3),'g');
plot(x,hip_circ(:,17),'c');
ylabel('Hip Circumduction (deg)');

% axis([0 100 -20 40]);
lh = legend('Baseline','Init. Exp.','Late Exp.','Washout');
set(lh,'FontSize',8)

subplot(3,1,3); hold on;
plot(x(2:end-1),torque(:,3),'g');
ylabel('Torque (Nm)');
xlabel('Gait Cycle (%)')
% set(gcf,'Position',[568 -167 544 769]);



figure(5); hold on;
% plot(trials, max(knee)/max(knee(:,2))*100,'.k')
plot(trials(3:17),max(knee(:,3:17))/max(knee(:,2))*100,'ok','MarkerSize',8);
plot(trials([1:2,18:22]),max(knee(:,[1:2,18:22]))/max(knee(:,2))*100,'ok','MarkerSize',8)
title('max knee')

figure(6); hold on;
% plot(trials,knee_vel,'or')
plot(trials(3:17),knee_vel_TO(3:17)/knee_vel_TO(2)*100,'ok','MarkerSize',8);
plot(trials([1:2,18:22]),knee_vel_TO([1:2,18:22])/knee_vel_TO(2)*100,'ok','MarkerSize',8)
title('knee flex and TO');

figure(7); hold on;
plot(trials(3:17),circumduction(3:17)/circumduction(2)*100,'ok','MarkerSize',8);
plot(trials([1:2,18:22]),circumduction([1:2,18:22])/circumduction(2)*100,'ok','MarkerSize',8)
title('circumduction')

figure(8);hold on;
% plot(trials,toeclear/toeclear(2)*100,'ok',);
plot(trials(3:17),toeclear(3:17)/toeclear(2)*100,'ok','MarkerSize',8);
plot(trials([1:2,18:22]),toeclear([1:2,18:22])/toeclear(2)*100,'ok','MarkerSize',8)
title('Max. Toe Height')

figure(9); hold on;
sym = steplength_l./(steplength_l+steplength_r);
plot(trials(3:17),sym(3:17),'.k')
plot(trials([1:2,18:22]),sym([1:2,18:22]),'.c')
title('step symmetry')



